<%--
  Created by IntelliJ IDEA.
  User: carol
  Date: 2018/6/16
  Time: 20:33
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.*,java.sql.*,javax.servlet.*,functest.*"%>
<html>
<head>
    <link type="text/css" rel="stylesheet" href="layui/css/layui.css" media="all">
    <script type="text/javascript" src="layui/layui.js"></script>
    <script type="text/javascript" src="js/jquery-3-2-1.js"></script>
    <meta charset="UTF-8">
</head>
<body>
<%
    request.setCharacterEncoding("UTF-8");
    String action = request.getParameter("action");
    String hb_name = request.getParameter("hbname");
    String driver="com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://localhost:3306/hbpw?useUnicode=true&characterEncoding=utf-8&useSSL=false";//地址
    String user="hbpwuser";
    String pass="LFFLXY";
    Class.forName(driver);                   //加载jdbc驱动
    Connection connection  = (Connection) DriverManager.getConnection(url,user,pass);
    FindAPrint func =new FindAPrint();
    if (action.equals("del")){//delete

        Statement statement2 = (Statement) connection.createStatement();  //连接实例
        String selectsql2 = "select count(*) as total from hd where HB_HBH ='" + hb_name + "';";
        ResultSet rst2 = statement2.executeQuery(selectsql2);
        if (rst2.next()){
            int totalhd = rst2.getInt("total");
            String checksql = "select count(*) as total from spxx where HB_HBH='" + hb_name + "';";
            ResultSet rst1 = statement2.executeQuery(checksql);
            if (rst1.next()){
                int hdtotal = rst1.getInt("total");
                if (hdtotal > 0) {
%>
<blockquote class="layui-elem-quote layui-quote-nm">无法删除，请删除有关航班所有购票信息</blockquote>
<%
    }
                else {

                Statement statement = (Statement) connection.createStatement();  //连接实例
                String delsql = "delete from hd where HB_HBH='" + hb_name + "' and HD_SXH="+ totalhd +";";
                try {
                    statement.execute(delsql);
%>
<blockquote class="layui-elem-quote layui-quote-nm">删除成功！</blockquote>
<%
                    }catch (SQLException e){
                        out.println(delsql);
                        out.println(e);
                    }
                }
            }
        }
    }
    else if (action.equals("edit")){//修改价格
                String sort_id = request.getParameter("sortid");
%>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>修改价格</legend>
</fieldset>
<form class="layui-form" method="post" style="margin: 20px" action="handlehd.jsp?action=changejg&sortid=<%=sort_id%>">
    <div class="layui-form-item">
        <label class="layui-form-label">航班名</label>
        <div class="layui-input-block">
            <input type="text" name="hbname" readonly value="<%=hb_name%>" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">头等舱价格</label>
        <div class="layui-input-block">
            <input type="number" min="1" name="topprice" lay-verify="number" placeholder="请输入" autocomplete="on" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">经济舱价格</label>
        <div class="layui-input-block">
            <input type="number" min="1" name="ecoprice" lay-verify="number" placeholder="请输入" autocomplete="on" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item" style="text-align: center;">
        <button class="layui-btn" lay-submit="">确认提交</button>
    </div>
</form>
<%
}
    else if (action.equals("changejg")){
        Statement statement = (Statement) connection.createStatement();  //连接实例
        String top_price = request.getParameter("topprice");
        String eco_price = request.getParameter("ecoprice");
        String sort_id = request.getParameter("sortid");
        String changesql = "update hd set HD_TDCJG=" + top_price + ", HD_JJCJG=" + eco_price + " where HB_HBH ='"
                + hb_name + "' and HD_SXH=" + sort_id + ";";
        try {
            statement.execute(changesql);
%>
<blockquote class="layui-elem-quote layui-quote-nm">更改成功！</blockquote>
<%
        }catch (SQLException e){
            out.println(changesql);
            out.println(e);
        }
    }
    else if (action.equals("add")){//新增
        Statement statement = (Statement) connection.createStatement();
        String findmax = "select count(HD_SXH) as maxsx from hd where HB_HBH = '" +hb_name+ "';";
        ResultSet rst = statement.executeQuery(findmax);
        rst.next();
        String maxport = "onlygodcangetsuchstring";
        int maxsx=rst.getInt("maxsx");
%>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>新增航段</legend>
</fieldset>
<form class="layui-form" method="post" style="margin: 20px" action="handlehd.jsp?action=afteradd">
    <div class="layui-form-item">
        <label class="layui-form-label">航班名</label>
        <div class="layui-input-block">
            <input type="text" name="hbname" readonly value="<%=hb_name%>" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">顺序号</label>
        <div class="layui-input-block">
            <input type="text" name="hdsxh" readonly value="<%=maxsx+1%>" class="layui-input">
        </div>
    </div>
    <%
        if (maxsx > 0){
            String getmaxidsql = "select JC_END from hd where HB_HBH = '" + hb_name + "' and HD_SXH=" + maxsx + ";";
            Statement statement2 = (Statement) connection.createStatement();
            ResultSet rst2 = statement2.executeQuery(getmaxidsql);
            rst2.next();
            maxport = func.Findjcname(rst2.getString("JC_END"));
    %>
    <div class="layui-form-item">
        <label class="layui-form-label">起始机场</label>
        <div class="layui-input-block">
            <input type="text" name="startport" readonly value="<%=maxport%>" class="layui-input">
        </div>
    </div>
    <%
        }
        else {
    %>
    <div class="layui-form-item">
        <label class="layui-form-label">起始机场</label>
        <div class="layui-input-block">
            <select id="start" name="startport" lay-verify="required">
                <option value=""></option>
                <%
                    List<String> portname=func.Findalljcname(maxport);
                    for(String attribute : portname) {
                        out.println("<option value=\""+attribute+"\">"+
                                attribute+"</option>");
                    }
                %>
            </select>
        </div>
    </div>
    <%
        }
    %>

    <div class="layui-form-item">
        <label class="layui-form-label">终点机场</label>
        <div class="layui-input-block">
            <select id="end" name="endport" lay-verify="required">
                <option value=""></option>
                <%
                    List<String> portname=func.Findalljcname(maxport);
                    for(String attribute : portname) {
                        out.println("<option value=\""+attribute+"\">"+
                                attribute+"</option>");
                    }
                %>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">头等舱价格</label>
        <div class="layui-input-block">
            <input type="number" min="1" name="topprice" lay-verify="number" placeholder="请输入" autocomplete="on" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">经济舱价格</label>
        <div class="layui-input-block">
            <input type="number" min="1" name="ecoprice" lay-verify="number" placeholder="请输入" autocomplete="on" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item" style="text-align: center;">
        <button class="layui-btn" lay-submit="" id="submit">确认提交</button>
    </div>
</form>
<%
    }
    else if (action.equals("afteradd")){
        Statement statement = (Statement) connection.createStatement();  //连接实例
        String top_price = request.getParameter("topprice");
        String eco_price = request.getParameter("ecoprice");
        String hdsxh = request.getParameter("hdsxh");
        String startportid = func.Findjc(request.getParameter("startport"));
        String endportid = func.Findjc(request.getParameter("endport"));
        if (startportid.equals(endportid)){
%>
<blockquote class="layui-elem-quote layui-quote-nm">起始机场与终点机场相同！</blockquote>
<%
        }
        else {
            String insertsql = "insert into hd values('" + hb_name + "'," + hdsxh + "," + startportid + ","
                    + endportid + "," + top_price + "," + eco_price + ");";
            try {
                statement.execute(insertsql);
%>
<blockquote class="layui-elem-quote layui-quote-nm">新增成功！</blockquote>
<%
            }catch (SQLException e){
                out.println(insertsql);
                out.println(e);
            }
        }
    }
%>
</body>
</html>
<script type="text/javascript">
    layui.use(['form','layer'],function (){
        var form = layui.form
            ,layer = layui.layer;
        form.verify({
            number: function(value, item){ //value：表单的值、item：表单的DOM对象
                if(!new RegExp("^[0-9\\s·]+$").test(value)){
                    return '请输个正常的数字';
                }
            }
        });
        form.render();
    })
</script>
